Wiki
Clone wikilifev-release / tutorial / Read_a_GetPot_datafile
Read a GetPot datafile
We now focus on how to read a GetPot input datafile. To this end we focus on the datafile example written in Write a GetPot datafile. Let's suppose we have a main.cpp file wherein we aim at reading the content of the datafile (whose filename is datafile.in
): to do that we need to
- include in the main file of the application the header files of the classes required.
- include in the main file of the application the lines of code which will actually read the input parameters.
The file to be included is lifev/core/filter/GetPot.hpp
. The GetPot class takes as input in its constructor the name of the datafile as a string. In the example reported, the instance of the GetPot object is called dataFile. When reading a variable one has to specify its path within the datafile and its default value (the latter used just in case in the datafile that variable is not present).
Below we report a main.cpp
file which reads an input GetPot datafile.
#include <Epetra ConfigDefs.h>
#ifdef EPETRAMPI
#include <Epetra MpiComm.h>
#else
#include <Epetra SerialComm.h>
#endif
#include <lifev/core/LifeV.hpp>
#include <lifev/core/filter/GetPot.hpp>
using namespace LifeV;
int main ( int argc, char** argv )
{
#ifdef HAVEMPI
MPI_Init ( &argc , &argv ) ;
std::shared_ptr< Epetra_Comm > Comm ( new Epetra_MpiComm ( MPI_COMM_WORLD ) ) ;
#else
std::shared_ptr< Epetra_Comm > Comm ( new Epetra_SerialComm ) ;
#endif
GetPot dataFile ( "datafile.in" );
std::string var1 = dataFile("section_AA/parameter_one", "default");
int var2 = dataFile("section_AA/parameter_two", 0) ;
int var3 = dataFile("section_AA/subsectionA/parameterA" , 1) ;
bool var4 = dataFile("section_AA/subsectionB/parameterB" , false ) ;
UInt size = dataFile.vector_variable_size("section_AA/subsectionB/subsubsectionA/parameterC") ;
std::vector<int> parameterC(size);
for ( UInt i = 0; i < size; ++i )
{
parameterC[i] = dataFile ( "section_AA/subsectionB/subsubsectionA/parameterC", 0, i);
}
double var6 = dataFile("section_BB/another_parameter", 0.2);
#ifdef HAVEMPI
MPI_Finalize () ;
#endif
return 0;
}
Here you can download the dataReadGetpot.in and exampleReadData.cpp files.
Updated